/****************************************************************
*****************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;

cap log close;
clear all;
drop _all;
set more 1;
pause on;

log using log_MSM_healthexp_infile.log, replace;

global gdtadir ".";

include do_globals.do;

/***********************************************
  Moments generated in Fortran program
  *2 stands for unanticipated case.
************************************************/
local lvmoments lfpr lnw sdlnw lnw_fd alfpr1to0 alfpr0to1 C ssa lfpr_diff_E2G lfpr_diff_G2B lfpr_diff_B2D lfpr_pt plnw41;


local lvprofiles A H AIME C I I_cond L labor lfpr lfpr_pt olabor inc lnw lnw_fe lnw_fe_full irecss ssa EV V0 V1 probV0 FS_spinc iFS;


local lvjvc = 0;
* get moments and individual from baseline and health experiments;
*foreach jv in 0 11 12 13 14 15 16 41 42 43 44 45 51 52 53 {;
foreach jv in 0 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 {;
   if (`jv'==0) {;
      local jvname "";
   };
   else {;
      local jvname "_CF`jv'";
   };

   * moments;
   prog_MSM_moments_infile "txt_MSM_Moments`jvname'.txt" "";
   *infile t `lvmoments' using txt_MSM_Moments`jvname'.txt, clear;
   gen byte cfid = `jv';
   ren *_f *;
   compress;
   if (`lvjvc'>0) {;
      append using  ${gdtadir}/dta_MSM_healthexp.dta;
   };
   save ${gdtadir}/dta_MSM_healthexp.dta, replace;

   * individuals;
   prog_MSM_indiv_infile "${cwd_indiv}/txt_MSM_Indiv`jvname'.txt";
   compress;

   preserve;
       gen cfid = `jv';
       compress;
       if (`lvjvc'>0) {;
           append using ${cwd_indiv}/dta_MSM_Indiv_healthexp.dta;
       };
       compress;
       sort cfid id t;
       save ${cwd_indiv}/dta_MSM_Indiv_healthexp.dta, replace;
   restore;

   
   collapse (mean) `lvprofiles' dlnw dlnw_full dlnw_base dlnw_full_base
            (sd) sdlnw=lnw (count) nn=id, by(t);
   sort t;
   replace dlnw = dlnw_base if t==${gvdata0}; 
   gen lnw_fd = sum(dlnw) if t>=${gvdata0};
      
   replace dlnw_full = dlnw_full_base if t==${gvdata0}; 
   gen lnw_fd_full = sum(dlnw_full) if t>=${gvdata0};  // here sum() does not include anything before ${gvdata0}

   gen cfid = `jv';
   compress;
   if (`lvjvc'>0) {;
      append using ${gdtadir}/dta_MSM_healthexp_Profiles.dta;
   };
   else {;
       local lvjvc = 1;
   };
   sort cfid t;
   save ${gdtadir}/dta_MSM_healthexp_Profiles.dta, replace;
};

***********************************************;
**  Moments from the sipp data;
** outcome: dta_sipp_Moments.dta;
************************************************;
use dta_sipp_Moments.dta, clear;
cap drop col;
ren *_d *;
gen byte cfid = 99;
compress;
append using  ${gdtadir}/dta_MSM_healthexp.dta;


foreach iv of local lvmoments {;
   ren `iv' m_`iv';
};
drop lfpr_exit lfpr1to0 lfpr0to1;

sort cfid t;
save ${gdtadir}/dta_MSM_healthexp.dta, replace;


* merge to get other profiles for the baseline and counterfactuals;
use cfid t `lvprofiles' lnw_fd lnw_fd_full using ${gdtadir}/dta_MSM_healthexp_Profiles.dta, clear;
sort cfid t;
merge 1:1 cfid t using ${gdtadir}/dta_MSM_healthexp.dta;
tab _merge;
*pause;
drop _merge;

sort cfid t;
save ${gdtadir}/dta_MSM_healthexp.dta, replace;

reshape wide m_* `lvprofiles' lnw_fd lnw_fd_full, i(t) j(cfid);
compress;
sort t;
save ${gdtadir}/dta_MSM_healthexp.dta, replace;

cap log close;





